package com.weplay.gameplane.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.weplay.gameplane.R;
import com.weplay.gameplane.utils.StringUtils;


/**
 * <P><B>Description: </B> 提示dialog  </P>
 * Revision Trail: (Date/Author/Description)
 * ${DATE} West Zheng CREATE
 *
 * @author West Zheng
 * @version 1.0
 */
public class AlertDialog extends Dialog implements View.OnClickListener {
    /**
     * The Tv title.
     */
    private TextView tvTitle;
    /**
     * The Tv message.
     */
    private TextView tvMessage;
    /**
     * The Tv ok.
     */
    private TextView tvOk;
    /**
     * The Tv cancel.
     */
    private TextView tvCancel;
    /**
     * The Ok listener.
     */
    private OnButtonClickListener okListener;
    /**
     * The Cancel listener.
     */
    private OnButtonClickListener cancelListener;
    /**
     * The M cancelable.
     */
    private boolean mCancelable = true;
    private View lineBtn;

    public AlertDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        initView(context);
    }

    public AlertDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_alert, null);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvMessage = (TextView) view.findViewById(R.id.tv_message);
        tvOk = (TextView) view.findViewById(R.id.tv_ok);
        tvOk.setOnClickListener(this);
        tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        lineBtn = view.findViewById(R.id.line_btn);
        tvCancel.setOnClickListener(this);
        tvCancel.setVisibility(View.GONE);
        lineBtn.setVisibility(View.GONE);

        setContentView(view);
        setCancelOnTouchOutside(false);
        setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                    if (mCancelable) {
                        dismiss();
                    } else {
                        return true;
                    }
                    if (cancelListener != null) {
                        cancelListener.onClick();
                    }
                }
                return false;
            }
        });
        setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (cancelListener != null) {
                    cancelListener.onClick();
                }
            }
        });
    }

    public AlertDialog setTitle(String title) {
        if (StringUtils.isNoEmpty(title)) {
            tvTitle.setText(title);
        } else {
            tvTitle.setVisibility(View.GONE);
            tvMessage.setMinLines(2);
        }
        return this;
    }

    public AlertDialog setMessageTextSize(float textSize) {
        tvMessage.setTextSize(textSize);
        return this;
    }

    public AlertDialog setTitleBold() {
        tvTitle.getPaint().setFakeBoldText(true);
        return this;
    }

    public AlertDialog setMessage(String message) {
        tvMessage.setText(message);
        tvMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
        return this;
    }

    public AlertDialog setOk(String ok, OnButtonClickListener listener) {
        tvOk.setText(ok);
        dismiss();
        this.okListener = listener;
        return this;
    }

    public AlertDialog setCancel(String cancel, OnButtonClickListener listener) {
        dismiss();
        tvCancel.setVisibility(View.VISIBLE);
        lineBtn.setVisibility(View.VISIBLE);
        tvCancel.setText(cancel);
        this.cancelListener = listener;
        return this;
    }


    //设置点击阴影部分，点击返回键是否可以取消
    public AlertDialog setOutCancel(boolean b) {
        setCancelable(b);
        return this;
    }

    //设置点击阴影部分是否可以取消，点返回键可以取消
    public AlertDialog setCancelOnTouchOutside(boolean b) {
        setCanceledOnTouchOutside(b);
        return this;
    }

    //设置点返回键可以不关闭
    public AlertDialog setDialogCancelable(boolean b) {
        setCancelable(b);
        return this;
    }

    @Override
    public void setCancelable(boolean flag) {
        mCancelable = flag;
        super.setCancelable(flag);
    }

    public AlertDialog setMessageGravity(int gravity) {
        tvMessage.setGravity(gravity);
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_ok) {
            if (okListener != null) {
                okListener.onClick();
            }
            dismiss();
        } else if (v.getId() == R.id.tv_cancel) {
            if (cancelListener != null) {
                cancelListener.onClick();
            }
            dismiss();
        }
    }

    /**
     * 描述 The interface On button click listener.
     *
     * @author Arron He
     * @created 2018 /09/02 16:01:25
     */
    public interface OnButtonClickListener {
        void onClick();
    }
}
